package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.merchant.domain.ImsYzzcSunGoods;
import com.ruoyi.merchant.domain.dto.ImsYzzcSunGoodsDto;
import com.ruoyi.merchant.domain.vo.ImsYzzcSunGoodsVo;
import com.ruoyi.merchant.service.IImsYzzcSunGoodsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆Controller
 * 
 * @author ruoyi
 * @date 2024-08-28
 */
@RestController
@RequestMapping("/merchant/good")
public class ImsYzzcSunGoodsController extends BaseController
{
    @Autowired
    private IImsYzzcSunGoodsService imsYzzcSunGoodsService;
    private static final String FILE_DELIMETER = ",";

    /**
     * 查询车辆列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImsYzzcSunGoods imsYzzcSunGoods)
    {
        startPage();
        List<ImsYzzcSunGoods> list = imsYzzcSunGoodsService.selectImsYzzcSunGoodsList(imsYzzcSunGoods);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('merchant:goods:list')")
    @GetMapping("/notImeilist")
    public TableDataInfo notImeiList(ImsYzzcSunGoods imsYzzcSunGoods)
    {
        List<ImsYzzcSunGoods> list = imsYzzcSunGoodsService.selectImsYzzcSunGoodsNotImeiList(imsYzzcSunGoods);
        return getDataTable(list);
    }

    /**
     * 导出车辆列表
     */
    @Log(title = "车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImsYzzcSunGoods imsYzzcSunGoods)
    {
        List<ImsYzzcSunGoods> list = imsYzzcSunGoodsService.selectImsYzzcSunGoodsList(imsYzzcSunGoods);
        ExcelUtil<ImsYzzcSunGoods> util = new ExcelUtil<ImsYzzcSunGoods>(ImsYzzcSunGoods.class);
        util.exportExcel(response, list, "车辆数据");
    }

    /**
     * 获取车辆详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imsYzzcSunGoodsService.selectImsYzzcSunGoodsById(id));
    }

    /**
     * 新增车辆
     */
    @PreAuthorize("@ss.hasPermi('merchant:goods:add')")
    @Log(title = "车辆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImsYzzcSunGoods imsYzzcSunGoods)
    {
        if (!imsYzzcSunGoodsService.checkUnique(imsYzzcSunGoods))
        {
            return error("新增参数'" + imsYzzcSunGoods.getCarnum() + "'失败，参数键名已存在");
        }
        return toAjax(imsYzzcSunGoodsService.insertImsYzzcSunGoods(imsYzzcSunGoods));
    }

    @GetMapping("filterImei")
    @PreAuthorize("@ss.hasPermi('merchant:goods:edit')")
    public AjaxResult filterImei(@RequestParam(required = false) String notImei,@RequestParam(required = false) String imei){

        return AjaxResult.success(imsYzzcSunGoodsService.filterImeiGoods(notImei, imei));
    }

    /**
     * 修改车辆
     */
    @PreAuthorize("@ss.hasPermi('merchant:goods:edit')")
    @Log(title = "车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImsYzzcSunGoods imsYzzcSunGoods)
    {
        if (!imsYzzcSunGoodsService.checkUnique(imsYzzcSunGoods))
        {
            return error("新增车辆失败，车辆车牌号已存在");
        }
        return toAjax(imsYzzcSunGoodsService.updateImsYzzcSunGoods(imsYzzcSunGoods));
    }

    /**
     * 删除车辆
     */
    @PreAuthorize("@ss.hasPermi('merchant:goods:remove')")
    @Log(title = "车辆", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imsYzzcSunGoodsService.deleteImsYzzcSunGoodsByIds(ids));
    }

    /**
     * 车辆年检图片上传
     */
    @Log(title = "车辆年检", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/checkPicFile")
    public AjaxResult checkPicFile(@RequestParam("checkfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String checkpic = FileUploadUtils.upload(RuoYiConfig.getVehiclePath(), file, MimeTypeUtils.IMAGE_EXTENSION);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", checkpic);
            return ajax;

        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 车辆行驶证上传
     */
    @Log(title = "车辆行驶证", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/drivingPicFile")
    public AjaxResult drivingPicFile(@RequestParam("drivingfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String drivingpic = FileUploadUtils.upload(RuoYiConfig.getVehiclePath(), file, MimeTypeUtils.IMAGE_EXTENSION);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", drivingpic);
            return ajax;

        }
        return error("上传图片异常，请联系管理员");
    }


    /**
     * 车辆图片上传
     */
    @Log(title = "车辆图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/files")
    @Anonymous
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
    {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty())
        {
            for (MultipartFile file : files){
                String vehicle = FileUploadUtils.upload(RuoYiConfig.getVehiclePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add(vehicle);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;

        }

        return error("上传图片异常，请联系管理员");
    }

    /**
     * 车辆主图上传
     */
    @Log(title = "车辆主图", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/file")
    @Anonymous
    public AjaxResult avatar(@RequestParam("vehiclefile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String vehicle = FileUploadUtils.upload(RuoYiConfig.getVehiclePath(), file, MimeTypeUtils.IMAGE_EXTENSION);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", vehicle);
            return ajax;

        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 车辆保险上传
     */
    @Log(title = "车辆保险", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/insurancePicFile")
    public AjaxResult insurancePicFile(@RequestParam("insurancefile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String insurancepic = FileUploadUtils.upload(RuoYiConfig.getVehiclePath(), file, MimeTypeUtils.IMAGE_EXTENSION);

            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", insurancepic);
            return ajax;

        }
        return error("上传图片异常，请联系管理员");
    }

    @Anonymous
    @GetMapping("/selectCarsBySchedule")
    public TableDataInfo selectCarsBySchedule(ImsYzzcSunGoodsDto imsYzzcSunGoods)
    {
        if(ObjectUtils.isNotEmpty(imsYzzcSunGoods.getStructureStr())){
            String[] structStr=imsYzzcSunGoods.getStructureStr().split(",");
            List<Long> structureList=new ArrayList<Long>();
            for(int i=0;i<structStr.length;i++){
                structureList.add(Long.valueOf(structStr[i]));
            }
            imsYzzcSunGoods.setStructureList(structureList);
        }
        if(ObjectUtils.isNotEmpty(imsYzzcSunGoods.getEngageCarTypeStr())){
            String[] structStr=imsYzzcSunGoods.getEngageCarTypeStr().split(",");
            List<Long> structureList=new ArrayList<Long>();
            for(int i=0;i<structStr.length;i++){
                structureList.add(Long.valueOf(structStr[i]));
            }
            imsYzzcSunGoods.setEngageCarTypeList(structureList);
        }
        if(ObjectUtils.isNotEmpty(imsYzzcSunGoods.getGrarboxStr())){
            String[] grarboxStr=imsYzzcSunGoods.getGrarboxStr().split(",");
            List<Long> grarboxList=new ArrayList<Long>();
            for(int i=0;i<grarboxStr.length;i++){
                grarboxList.add(Long.valueOf(grarboxStr[i]));
            }
            imsYzzcSunGoods.setGrarboxList(grarboxList);
        }
        if(ObjectUtils.isNotEmpty(imsYzzcSunGoods.getCartypeStr())){
            String[] cartypeStr=imsYzzcSunGoods.getCartypeStr().split(",");
            List<Long> cartypeList=new ArrayList<Long>();
            for(int i=0;i<cartypeStr.length;i++){
                cartypeList.add(Long.valueOf(cartypeStr[i]));
            }
            imsYzzcSunGoods.setCartypeList(cartypeList);
        }
        if(ObjectUtils.isNotEmpty(imsYzzcSunGoods.getStatusStr())){
            String[] statusStr=imsYzzcSunGoods.getStatusStr().split(",");
            List<Long> statusList=new ArrayList<Long>();
            for(int i=0;i<statusStr.length;i++){
                statusList.add(Long.valueOf(statusStr[i]));
            }
            imsYzzcSunGoods.setStatusList(statusList);
        }

        Long userId=null;
        startPage();
        List<ImsYzzcSunGoodsVo> list = imsYzzcSunGoodsService.selectCarsBySchedule(imsYzzcSunGoods);
        for(ImsYzzcSunGoodsVo goods:list){
            String structureStr="";
            if(goods.getStructure()==1){
                structureStr="两厢";
            }else{
                structureStr="三厢";
            }
            String grarboxStr="";
            if(goods.getGrarbox()==1){
                grarboxStr="手动";
            }else{
                grarboxStr="自动";
            }
            String brandStr=getBrand(goods.getBrand());
            goods.setVehicleParams(new String[]{goods.getColour(),structureStr,grarboxStr,goods.getDisplacement(),brandStr});
            if(goods.getCartype()==1){
                goods.setCarTypePic("/prod-api/profile/vehicle/suv_20240416144040.jpg");
            }
            if(goods.getCartype()==2){
                goods.setCarTypePic("/prod-api/profile/vehicle/jc_20240416144128.jpg");
            }
            if(goods.getCartype()==3){
                goods.setCarTypePic("/prod-api/profile/vehicle/cdxny_20240416144029.png");
            }
            if(goods.getCartype()==4){
                goods.setCarTypePic("/prod-api/profile/vehicle/ydhd_20240416144047.jpg");
            }
            if(goods.getCartype()==5){
                goods.setCarTypePic("/prod-api/profile/vehicle/sw_20240416144117.jpg");
            }
            if(goods.getCartype()==6){
                goods.setCarTypePic("/prod-api/profile/vehicle/fc_20240416144139.jpg");
            }
            if(goods.getCartype()==7){
                goods.setCarTypePic("/prod-api/profile/vehicle/daba_20240416144143.jpg");
            }
            if(goods.getCartype()==8){
                goods.setCarTypePic("/prod-api/profile/vehicle/cp_20240416144133.jpg");
            }
        }
        return getDataTable(list);
    }

    public String getBrand(Long brand){

        String brandStr="";
        if (ObjectUtils.isNotEmpty(brand)) {
            if (brand == 1) {
                brandStr = "大众";
            }
            if (brand == 2) {
                brandStr = "奔驰";
            }
            if (brand == 3) {
                brandStr = "丰田";
            }
            if (brand == 4) {
                brandStr = "宝马";
            }
            if (brand == 5) {
                brandStr = "奥迪";
            }
            if (brand == 6) {
                brandStr = "比亚迪";
            }
            if (brand == 7) {
                brandStr = "本田";
            }
            if (brand == 8) {
                brandStr = "别克";
            }
            if (brand == 9) {
                brandStr = "福特";
            }
            if (brand == 10) {
                brandStr = "哈弗";
            }
            if (brand == 11) {
                brandStr = "埃尔法";
            }
            if (brand == 12) {
                brandStr = "腾势";
            }
            if (brand == 13) {
                brandStr = "劳斯莱斯";
            }
            if (brand == 14) {
                brandStr = "保时捷";
            }
            if (brand == 15) {
                brandStr = "路虎";
            }
            if (brand == 16) {
                brandStr = "东风";
            }
            if (brand == 17) {
                brandStr = "凯迪拉克";
            }
            if (brand == 18) {
                brandStr = "马自达";
            }
            if (brand == 19) {
                brandStr = "奇瑞";
            }
            if (brand == 20) {
                brandStr = "玛莎拉蒂";
            }
            if (brand == 21) {
                brandStr = "雪佛兰";
            }
            if (brand == 22) {
                brandStr = "宾利";
            }
            if (brand == 23) {
                brandStr = "起亚";
            }
            if (brand == 24) {
                brandStr = "雪铁龙";
            }
            if (brand == 25) {
                brandStr = "MINI";
            }
            if (brand == 26) {
                brandStr = "现代";
            }
        }
        return brandStr;

    }
}
